<template>
  <div class="app-container">
    <el-form inline style="margin: 10px;">
      <el-form-item label="搜索">
        <el-input v-model="tempSearchObj.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <!-- 查询与重置按钮 -->
      <el-form-item>
        <el-button type="primary" icon="search"   >查询</el-button>
        <el-button type="primary" icon="refresh" @click="resetSearch" >重置</el-button>
      </el-form-item>
    </el-form>

    <div style="margin: 5px 0px;">
      <el-button type="primary" icon="plus" >添加新用户</el-button>
      <el-button type="danger" icon="delete" :disabled="selectedIds.length === 0" >批量删除</el-button>
    </div>

      <el-table border stripe v-loading="listLoading" 
      :data="users" style="width: 100%;" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index"  label="序号" width="80" align="center" />
        <el-table-column prop="username" label="用户名" width="150" />
      <el-table-column prop="nickName" label="用户昵称" />
      <el-table-column prop="roleName" label="权限列表" />
      <el-table-column prop="gmtCreate" label="创建时间" width="180"/>
      <el-table-column prop="gmtModified" label="更新时间" width="180"/>
      <el-table-column label="操作" width=" " align="center">
        <template #default="{ row }">
          <el-button type="info" 
          icon="el-icon-view" title="分配角色"
           @click="showAssignRole(row)" />
          <el-button
          type="primary"
            size="mini"
            icon="edit"
            title="修改用户"
            @click="showUpdateUser(row)"
          />
          <el-popconfirm
            :title="`确认删除${row.username}用户？`"
            icon="el-icon-delete"
            @confirm="handleDeleteUser(row)"
          >
            <el-button type="danger" title="删除用户" size="mini" icon="el-icon-delete" />
          </el-popconfirm>
        </template>
      </el-table-column>
      </el-table>

       <!-- 分页器 -->
    <el-pagination
      style="margin-top: 20px; text-align: center"
      :current-page="page"
      :total="total"
      :page-size="limit"
      :pager-count="7"
      :page-sizes="[3, 5, 10, 20]"
      background="true"
      layout="prev, pager, next, jumper,->,sizes,total"
       @current-change="getRoles()"
      @size-change="handleSizeChange"
    />


    <!-- 弹窗 -->
    <!-- <el-dialog -->


  </div>
</template>

<script>
export default {
  name: 'UserList',
  data() {
    return {
      search: '',
      listLoading: false, // 是否显示列表加载的提示
      searchObj: { // 包含请求搜索条件数据的对象
        username: ''
      },
      tempSearchObj: { // 收集搜索条件输入的对象
        username: ''
      },
      selectedIds: [], // 所有选择的user的id的数组
      users: [], // 当前页的用户列表
      page: 1, // 当前页码
      limit: 3, // 每页数量
      total: 0, // 总数量
      user: {}, // 当前要操作的user
      dialogUserVisible: false, // 是否显示用户添加/修改的dialog
      userRules: { // 用户添加/修改表单的校验规则
        username: [
          { required: true, message: '用户名必须输入' },
          { min: 4, message: '用户名不能小于4位' }
        ],
        password: [
          { required: true, validator: this.validatePassword }
        ]
      },
      loading: false, // 是否正在提交请求中
      dialogRoleVisible: false, // 是否显示角色Dialog
      allRoles: [], // 所有角色列表
      userRoleIds: [], // 用户的角色ID的列表
      isIndeterminate: false, // 是否是不确定的
      checkAll: false // 是否全选
    }
  },
  created() {
    // this.getUsers()
  },
  methods: {

    // 重置按钮
    resetSearch() {
      this.searchObj.username = ''
      this.tempSearchObj.username = ''
      // this.getUsers() 
    this.getRoles()
    },



     /*
    显示指定角色的界面
    */
    // showAssignRole(user) {
    //   this.user = user
    //   this.dialogRoleVisible = true
    //   this.getRoles()
    // },

   /*
    异步获取用户的角色列表
    */
   async getRoles() {
    const res = await this.$API.user.getRoles(this.user.id) 
    const { allRolesList,assignRoles } = res.data // 角色列表
    this.allRoles = allRolesList  // 所有角色列表
    this.userRoleIds = assignRoles.map(item => item.id)  // 用户的角色ID的列表
    this.isIndeterminate = assignRoles.length > 0 && assignRoles.length < allRolesList.length  // 是否是不确定状态
   },


        /*
    处理pageSize发生改变的监听回调
    */
    handleSizeChange(pageSize) {
      this.limit = pageSize
      this.getUsers()
    },
  }


}
</script>

<style scoped>
.el-pagination__rightwrapper {
  display: flex;
  justify-content: flex-start;
}

</style>